
package chatclient.applets.canvas;

import java.awt.*;
import java.awt.event.ComponentEvent;
import java.awt.event.ComponentListener;
import javax.swing.JComponent;

public abstract class ImageCanvas extends JComponent
        implements ComponentListener {

    public ImageCanvas(Image image1) {
        this(image1, 2, 2);
    }

    public ImageCanvas(Image image1, int i, int j) {
        sync = new Object();
        image = image1;
        hFit = i;
        vFit = j;
        prefWidth = 1;
        prefHeight = 1;

        flushScaled();
        addComponentListener(this);
    }

    public void destroy() {
        setVisible(false);
    }

    public boolean isDestroyed() {
        return !isVisible();
    }

    public void setImage(Image image1) {
        image = image1;
        synchronized (sync) {
            prefWidth = Math.max(1, prefWidth);
            prefHeight = Math.max(1, prefHeight);
            flushScaled();
        }
        updateSize();
        repaint();
    }

    private void flushScaled() {
        if (imageScaled != null) {
            imageScaled.flush();
            imageScaled = null;
        }
    }

    protected void updateSize() {
        invalidate();
        Object obj = this;
        for (Container container = getParent(); container != null && container != obj; container = container.getParent()) {
            obj = container;
        }

        validate();
        ((Component) obj).validate();
    }

    @Override
    public void paintComponent(Graphics g) {

        super.paintComponent(g);
        
        if (isDestroyed()) {
            return;
        }
        boolean flag = false;
        synchronized (sync) {
            int i = getSize().width;
            int j = getSize().height;
            int k;
            int l;
            if (i >= 0 && j >= 0 && image != null && (k = image.getWidth(this)) > 0 && (l = image.getHeight(this)) > 0) {
                int i1 = (int) ((float) j * ((float) k / (float) l));
                int j1 = (int) ((float) i * ((float) l / (float) k));
                int k1 = fitSize(hFit, k, i, i1);
                int l1 = fitSize(vFit, l, j, j1);
                if (k == k1 && l == l1) {
                    g.drawImage(image, 0, 0, getBackground(), this);
                } else {
                    if (imageScaled == null || imageScaled.getWidth(null) != k1 || imageScaled.getHeight(null) != l1) {
                        flushScaled();
                        imageScaled = image.getScaledInstance(k1, l1, 1);
                    }
                    g.drawImage(imageScaled, 0, 0, getBackground(), this);
                }
                flag = k != prefWidth || l != prefHeight;
                prefWidth = k;
                prefHeight = l;
            }
        }
        if (flag) {
            updateSize();
        }
    }

    private int fitSize(int i, int j, int k, int l) {
        switch (i) {
            case 1:
                return j;

            case 2:
                return k;

            case 3:
                return Math.min(l, j);

            case 4:
                return Math.max(l, j);
        }
        return j;
    }

    @Override
    public Dimension getPreferredSize() {
        return new Dimension(prefWidth, prefHeight);
    }

    @Override
    public void componentHidden(ComponentEvent componentevent1) {
    }

    @Override
    public void componentShown(ComponentEvent componentevent1) {
    }

    @Override
    public void componentMoved(ComponentEvent componentevent1) {
    }

    @Override
    public void componentResized(ComponentEvent componentevent) {
        synchronized (sync) {
            flushScaled();
        }
        repaint();
    }
    public static final int FIT_NONE = 1;
    public static final int FIT_EXACT = 2;
    public static final int FIT_SMALL = 3;
    public static final int FIT_BIG = 4;
    private Image image;
    private int prefWidth;
    private int prefHeight;
    private Image imageScaled;
    private int hFit;
    private int vFit;
    private final Object sync;
}
